/*   1:    */ package org.intellij.plugins.relaxNG.compact.psi.impl;
/*   2:    */ 
/*   3:    */

import com.intellij.codeInsight.CodeInsightUtilCore;
import com.intellij.codeInsight.daemon.EmptyResolveMessageProvider;
import com.intellij.codeInsight.lookup.LookupItem;
import com.intellij.codeInsight.template.*;
import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.codeInspection.LocalQuickFixProvider;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.lang.ASTNode;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.OpenFileDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFileFactory;
import com.intellij.psi.PsiReference;
import com.intellij.psi.ResolveState;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.scope.BaseScopeProcessor;
import com.intellij.psi.tree.IElementType;
import com.intellij.util.ArrayUtil;
import com.intellij.util.IncorrectOperationException;
import org.intellij.plugins.relaxNG.compact.RncElementTypes;
import org.intellij.plugins.relaxNG.compact.RncFileType;
import org.intellij.plugins.relaxNG.compact.RncTokenTypes;
import org.intellij.plugins.relaxNG.compact.psi.*;
import org.intellij.plugins.relaxNG.compact.psi.util.EscapeUtil;
import org.intellij.plugins.relaxNG.compact.psi.util.RenameUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/*   4:    */
/*   5:    */
/*   6:    */
/*   7:    */
/*   8:    */
/*   9:    */
/*  10:    */
/*  11:    */
/*  12:    */
/*  13:    */
/*  14:    */
/*  15:    */
/*  16:    */
/*  17:    */
/*  18:    */
/*  19:    */
/*  20:    */
/*  21:    */
/*  22:    */
/*  23:    */
/*  24:    */
/*  25:    */
/*  26:    */
/*  27:    */
/*  28:    */
/*  29:    */
/*  30:    */
/*  31:    */
/*  32:    */
/*  33:    */
/*  34:    */
/*  35:    */
/*  36:    */
/*  37:    */
/*  38:    */
/*  39:    */
/*  40:    */
/*  41:    */
/*  42:    */
/*  43:    */
/*  44:    */
/*  45:    */
/*  46:    */
/*  47:    */ 
/*  48:    */ public class RncNameImpl
/*  49:    */   extends RncElementImpl
/*  50:    */   implements RncName, PsiReference, EmptyResolveMessageProvider, LocalQuickFixProvider
/*  51:    */ {
/*  52:    */   public RncNameImpl(ASTNode paramASTNode)
/*  53:    */   {
/*  54: 65 */     super(paramASTNode);
/*  55:    */   }
/*  56:    */   
/*  57:    */   @Nullable
/*  58:    */   public String getPrefix()
/*  59:    */   {
/*  60: 70 */     String[] arrayOfString = EscapeUtil.unescapeText(getNode()).split(":", 2);
/*  61: 71 */     return arrayOfString.length == 2 ? arrayOfString[0] : null;
/*  62:    */   }
/*  63:    */   
/*  64:    */   @NotNull
/*  65:    */   public String getLocalPart()
/*  66:    */   {
/*  67: 76 */     String[] arrayOfString = EscapeUtil.unescapeText(getNode()).split(":", 2); String 
/*  68: 77 */       tmp29_28 = (arrayOfString.length == 1 ? arrayOfString[0] : arrayOfString[1]);
/*  69: 77 */     if (tmp29_28 == null) {
/*  70: 77 */       throw new IllegalStateException(String.format("@NotNull method %s.%s must not return null", new Object[] { "org/intellij/plugins/relaxNG/compact/psi/impl/RncNameImpl", "getLocalPart" }));
/*  71:    */     }
/*  72: 77 */     return tmp29_28;
/*  73:    */   }
/*  74:    */   
/*  75:    */   public void accept(@NotNull RncElementVisitor paramRncElementVisitor)
/*  76:    */   {
/*  77: 82 */     if (paramRncElementVisitor == null) {
/*  78: 82 */       throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "0", "org/intellij/plugins/relaxNG/compact/psi/impl/RncNameImpl", "accept" }));
/*  79:    */     }
/*  80: 82 */     paramRncElementVisitor.visitName(this);
/*  81:    */   }
/*  82:    */   
/*  83:    */   public PsiReference getReference()
/*  84:    */   {
/*  85: 87 */     return getPrefix() == null ? null : this;
/*  86:    */   }
/*  87:    */   
/*  88:    */   public PsiElement getElement()
/*  89:    */   {
/*  90: 91 */     return this;
/*  91:    */   }
/*  92:    */   
/*  93:    */   public TextRange getRangeInElement()
/*  94:    */   {
/*  95: 95 */     return TextRange.from(0, getText().indexOf(':'));
/*  96:    */   }
/*  97:    */   
/*  98:    */   @Nullable
/*  99:    */   public PsiElement resolve()
/* 100:    */   {
/* 101:100 */     MyResolver localMyResolver = new MyResolver(getPrefix(), a());
/* 102:101 */     getContainingFile().processDeclarations(localMyResolver, ResolveState.initial(), this, this);
/* 103:102 */     return localMyResolver.getResult();
/* 104:    */   }
/* 105:    */   
/* 106:    */   private Kind a()
/* 107:    */   {
/* 108:106 */     IElementType localIElementType = getNode().getTreeParent().getElementType();
/* 109:107 */     if (localIElementType == RncElementTypes.DATATYPE_PATTERN) {
/* 110:108 */       return Kind.DATATYPES;
/* 111:    */     }
/* 112:110 */     return Kind.NAMESPACE;
/* 113:    */   }
/* 114:    */   
/* 115:    */   @NotNull
/* 116:    */   public String getCanonicalText()
/* 117:    */   {
/* 118:116 */     String tmp11_8 = getRangeInElement().substring(getText());
/* 119:116 */     if (tmp11_8 == null) {
/* 120:116 */       throw new IllegalStateException(String.format("@NotNull method %s.%s must not return null", new Object[] { "org/intellij/plugins/relaxNG/compact/psi/impl/RncNameImpl", "getCanonicalText" }));
/* 121:    */     }
/* 122:116 */     return tmp11_8;
/* 123:    */   }
/* 124:    */   
/* 125:    */   public PsiElement handleElementRename(String paramString)
/* 126:    */     throws IncorrectOperationException
/* 127:    */   {
/* 128:120 */     ASTNode localASTNode1 = getNode();
/* 129:121 */     ASTNode localASTNode2 = RenameUtil.createPrefixedNode(getManager(), paramString, getLocalPart());
/* 130:122 */     localASTNode1.getTreeParent().replaceChild(localASTNode1, localASTNode2);
/* 131:123 */     return localASTNode2.getPsi();
/* 132:    */   }
/* 133:    */   
/* 134:    */   public PsiElement bindToElement(@NotNull PsiElement paramPsiElement)
/* 135:    */     throws IncorrectOperationException
/* 136:    */   {
/* 137:127 */     if (paramPsiElement == null) {
/* 138:127 */       throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "0", "org/intellij/plugins/relaxNG/compact/psi/impl/RncNameImpl", "bindToElement" }));
/* 139:    */     }
/* 140:127 */     throw new UnsupportedOperationException();
/* 141:    */   }
/* 142:    */   
/* 143:    */   public boolean isReferenceTo(PsiElement paramPsiElement)
/* 144:    */   {
/* 145:131 */     return ((paramPsiElement instanceof RncElement)) && (Comparing.equal(resolve(), paramPsiElement));
/* 146:    */   }
/* 147:    */   
/* 148:    */   @NotNull
/* 149:    */   public Object[] getVariants()
/* 150:    */   {
/* 151:136 */     Object[] tmp3_0 = ArrayUtil.EMPTY_OBJECT_ARRAY;
/* 152:136 */     if (tmp3_0 == null) {
/* 153:136 */       throw new IllegalStateException(String.format("@NotNull method %s.%s must not return null", new Object[] { "org/intellij/plugins/relaxNG/compact/psi/impl/RncNameImpl", "getVariants" }));
/* 154:    */     }
/* 155:136 */     return tmp3_0;
/* 156:    */   }
/* 157:    */   
/* 158:    */   public boolean isSoft()
/* 159:    */   {
/* 160:140 */     String str = getPrefix();
/* 161:141 */     return ("xsd".equals(str)) || ("xml".equals(str));
/* 162:    */   }
/* 163:    */   
/* 164:    */   @NotNull
/* 165:    */   public String getUnresolvedMessagePattern()
/* 166:    */   {
/* 167:146 */     String tmp2_0 = "Unresolved namespace prefix ''{0}''";
/* 168:146 */     if (tmp2_0 == null) {
/* 169:146 */       throw new IllegalStateException(String.format("@NotNull method %s.%s must not return null", new Object[] { "org/intellij/plugins/relaxNG/compact/psi/impl/RncNameImpl", "getUnresolvedMessagePattern" }));
/* 170:    */     }
/* 171:146 */     return tmp2_0;
/* 172:    */   }
/* 173:    */   
/* 174:    */   @Nullable
/* 175:    */   public LocalQuickFix[] getQuickFixes()
/* 176:    */   {
/* 177:152 */     if (getPrefix() != null) {
/* 178:153 */       return new LocalQuickFix[] { new CreateDeclFix(this) };
/* 179:    */     }
/* 180:155 */     return LocalQuickFix.EMPTY_ARRAY;
/* 181:    */   }
/* 182:    */   
/* 183:    */   private static enum Kind
/* 184:    */   {
/* 185:    */     private Kind() {}
/* 186:    */   }
/* 187:    */   
/* 188:    */   private static class MyResolver
/* 189:    */     extends BaseScopeProcessor
/* 190:    */   {
/* 191:    */     private final String a;
/* 192:    */     private final RncNameImpl.Kind b;
/* 193:    */     private PsiElement c;
/* 194:    */     
/* 195:    */     public MyResolver(String paramString, RncNameImpl.Kind paramKind)
/* 196:    */     {
/* 197:164 */       this.a = paramString;
/* 198:165 */       this.b = paramKind;
/* 199:    */     }
/* 200:    */     
/* 201:    */     public boolean execute(@NotNull PsiElement paramPsiElement, ResolveState paramResolveState)
/* 202:    */     {
/* 203:169 */       if (paramPsiElement == null) {
/* 204:169 */         throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "0", "org/intellij/plugins/relaxNG/compact/psi/impl/RncNameImpl$MyResolver", "execute" }));
/* 205:    */       }
/* 206:169 */       ASTNode localASTNode = paramPsiElement.getNode();
/* 207:170 */       if (localASTNode == null) {
/* 208:170 */         return true;
/* 209:    */       }
/* 210:172 */       if (!(paramPsiElement instanceof RncDecl)) {
/* 211:173 */         return false;
/* 212:    */       }
/* 213:176 */       IElementType localIElementType = localASTNode.getElementType();
/* 214:177 */       if ((this.b == RncNameImpl.Kind.NAMESPACE) && (localIElementType == RncElementTypes.NS_DECL))
/* 215:    */       {
/* 216:178 */         if (a(paramPsiElement)) {
/* 217:178 */           return false;
/* 218:    */         }
/* 219:    */       }
/* 220:179 */       else if ((this.b == RncNameImpl.Kind.DATATYPES) && (localIElementType == RncElementTypes.DATATYPES_DECL) && 
/* 221:180 */         (a(paramPsiElement))) {
/* 222:180 */         return false;
/* 223:    */       }
/* 224:183 */       return true;
/* 225:    */     }
/* 226:    */     
/* 227:    */     private boolean a(PsiElement paramPsiElement)
/* 228:    */     {
/* 229:187 */       if (this.a.equals(((RncDecl)paramPsiElement).getPrefix()))
/* 230:    */       {
/* 231:188 */         this.c = paramPsiElement;
/* 232:189 */         return true;
/* 233:    */       }
/* 234:191 */       return false;
/* 235:    */     }
/* 236:    */     
/* 237:    */     public PsiElement getResult()
/* 238:    */     {
/* 239:195 */       return this.c;
/* 240:    */     }
/* 241:    */   }
/* 242:    */   
/* 243:    */   public static class CreateDeclFix
/* 244:    */     implements LocalQuickFix
/* 245:    */   {
/* 246:    */     private final RncNameImpl a;
/* 247:    */     
/* 248:    */     public CreateDeclFix(RncNameImpl paramRncNameImpl)
/* 249:    */     {
/* 250:203 */       this.a = paramRncNameImpl;
/* 251:    */     }
/* 252:    */     
/* 253:    */     @NotNull
/* 254:    */     public String getName()
/* 255:    */     {
/* 256:208 */       String tmp37_31 = (getFamilyName() + " '" + this.a.getPrefix() + "'");
/* 257:208 */       if (tmp37_31 == null) {
/* 258:208 */         throw new IllegalStateException(String.format("@NotNull method %s.%s must not return null", new Object[] { "org/intellij/plugins/relaxNG/compact/psi/impl/RncNameImpl$CreateDeclFix", "getName" }));
/* 259:    */       }
/* 260:208 */       return tmp37_31;
/* 261:    */     }
/* 262:    */     
/* 263:    */     @NotNull
/* 264:    */     public String getFamilyName()
/* 265:    */     {
/* 266:213 */       String tmp36_30 = ("Create " + this.a.a().name().toLowerCase() + " declaration");
/* 267:213 */       if (tmp36_30 == null) {
/* 268:213 */         throw new IllegalStateException(String.format("@NotNull method %s.%s must not return null", new Object[] { "org/intellij/plugins/relaxNG/compact/psi/impl/RncNameImpl$CreateDeclFix", "getFamilyName" }));
/* 269:    */       }
/* 270:213 */       return tmp36_30;
/* 271:    */     }
/* 272:    */     
/* 273:    */     public void applyFix(@NotNull Project paramProject, @NotNull ProblemDescriptor paramProblemDescriptor)
/* 274:    */     {
/* 275:218 */       if (paramProject == null) {
/* 276:218 */         throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "0", "org/intellij/plugins/relaxNG/compact/psi/impl/RncNameImpl$CreateDeclFix", "applyFix" }));
/* 277:    */       }
/* 278:218 */       if (paramProblemDescriptor == null) {
/* 279:218 */         throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "1", "org/intellij/plugins/relaxNG/compact/psi/impl/RncNameImpl$CreateDeclFix", "applyFix" }));
/* 280:    */       }
/* 281:218 */       String str = this.a.getPrefix();
/* 282:219 */       PsiFileFactory localPsiFileFactory = PsiFileFactory.getInstance(this.a.getProject());
/* 283:220 */       RncFile localRncFile1 = (RncFile)localPsiFileFactory.createFileFromText("dummy.rnc", RncFileType.getInstance(), this.a.a().name().toLowerCase() + " " + str + " = \"###\"");
/* 284:    */       
/* 285:    */ 
/* 286:223 */       RncFile localRncFile2 = (RncFile)this.a.getContainingFile();
/* 287:224 */       RncDecl[] arrayOfRncDecl = localRncFile2.getDeclarations();
/* 288:225 */       RncDecl localRncDecl1 = localRncFile1.getDeclarations()[0];
/* 289:    */       RncDecl localRncDecl2;
/* 290:227 */       if (arrayOfRncDecl.length > 0)
/* 291:    */       {
/* 292:228 */         localRncDecl2 = (RncDecl)localRncFile2.addAfter(localRncDecl1, arrayOfRncDecl[(arrayOfRncDecl.length - 1)]);
/* 293:    */       }
/* 294:    */       else
/* 295:    */       {
/* 296:230 */         localObject = localRncFile2.getGrammar();
/* 297:231 */         if (localObject != null) {
/* 298:232 */           localRncDecl2 = (RncDecl)localRncFile2.addBefore(localRncDecl1, (PsiElement)localObject);
/* 299:    */         } else {
/* 300:234 */           localRncDecl2 = (RncDecl)localRncFile2.add(localRncDecl1);
/* 301:    */         }
/* 302:    */       }
/* 303:238 */       Object localObject = localRncDecl2.getParent().getNode();
/* 304:239 */       assert (localObject != null);
/* 305:    */       
/* 306:241 */       ASTNode localASTNode1 = localRncDecl2.getNode();
/* 307:242 */       assert (localASTNode1 != null);
/* 308:    */       
/* 309:244 */       CodeStyleManager.getInstance(localRncDecl2.getManager().getProject()).reformatNewlyAddedElement((ASTNode)localObject, localASTNode1);
/* 310:    */       
/* 311:246 */       PsiElement localPsiElement = localRncDecl2.getLastChild();
/* 312:247 */       assert (localPsiElement != null);
/* 313:    */       
/* 314:249 */       ASTNode localASTNode2 = localPsiElement.getNode();
/* 315:250 */       assert (localASTNode2 != null);
/* 316:    */       
/* 317:252 */       assert (localASTNode2.getElementType() == RncTokenTypes.LITERAL);
/* 318:    */       
/* 319:254 */       int i = localPsiElement.getTextRange().getStartOffset();
/* 320:    */       
/* 321:256 */       localPsiElement.delete();
/* 322:    */       
/* 323:258 */       VirtualFile localVirtualFile = this.a.getElement().getContainingFile().getVirtualFile();
/* 324:259 */       if (localVirtualFile != null)
/* 325:    */       {
/* 326:260 */         Editor localEditor = FileEditorManager.getInstance(paramProject).openTextEditor(new OpenFileDescriptor(paramProject, localVirtualFile, i), true);
/* 327:261 */         if (localEditor != null)
/* 328:    */         {
/* 329:262 */           RncDecl localRncDecl3 = (RncDecl)CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(localRncDecl2);
/* 330:    */           
/* 331:264 */           TemplateManager localTemplateManager = TemplateManager.getInstance(paramProject);
/* 332:265 */           Template localTemplate = localTemplateManager.createTemplate("", "");
/* 333:266 */           localTemplate.addTextSegment(" \"");
/* 334:267 */           Expression local1 = new Expression()
/* 335:    */           {
/* 336:    */             public Result calculateResult(ExpressionContext paramAnonymousExpressionContext)
/* 337:    */             {
/* 338:269 */               return new TextResult("");
/* 339:    */             }
/* 340:    */             
/* 341:    */             public Result calculateQuickResult(ExpressionContext paramAnonymousExpressionContext)
/* 342:    */             {
/* 343:273 */               return calculateResult(paramAnonymousExpressionContext);
/* 344:    */             }
/* 345:    */             
/* 346:    */             public LookupItem[] calculateLookupItems(ExpressionContext paramAnonymousExpressionContext)
/* 347:    */             {
/* 348:277 */               return LookupItem.EMPTY_ARRAY;
/* 349:    */             }
/* 350:279 */           };
/* 351:280 */           localTemplate.addVariable("uri", local1, local1, true);
/* 352:281 */           localTemplate.addTextSegment("\"");
/* 353:282 */           localTemplate.addEndVariable();
/* 354:    */           
/* 355:284 */           localEditor.getCaretModel().moveToOffset(localRncDecl3.getTextRange().getEndOffset());
/* 356:285 */           localTemplateManager.startTemplate(localEditor, localTemplate);
/* 357:    */         }
/* 358:    */       }
/* 359:    */     }
/* 360:    */   }
/* 361:    */ }



/* Location:           E:\idea\

 * Qualified Name:     org.intellij.plugins.relaxNG.compact.psi.impl.RncNameImpl

 * JD-Core Version:    0.7.0.1

 */